package com.kingyun.gpsinspection.purificationservice.services.som.innerservice.impl;

import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.SomFileMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.File;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.SomFileService;
import com.kingyun.gpsinspection.purificationservice.services.hbaseservice.HbaseUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.util.*;

/**
 * Created by yangcs on 2017/1/19.
 */
public class SomFileServiceImpl implements SomFileService {
    @Autowired
    private SomFileMapper somFileMapper;

    private static Logger logger = LoggerFactory.getLogger(SomFileServiceImpl.class);

    /**
     * 根据业务id查询图片
     *
     * @param businessId
     * @return
     */
    @Override
    public List<String> findByBusinessId(String businessId) {
        Map map = new HashMap<>();
        map.put("businessId", businessId);
        return somFileMapper.findByBusinessId(map);
    }

    /**
     * 根据业务id和类型查询图片
     *
     * @param businessId
     * @param type
     * @return
     */
    @Override
    public List<File> findByBusiness(String businessId, int type) {
        Map map = new HashMap<>();
        map.put("businessId", businessId);
        map.put("type", type);
        return somFileMapper.findByBusiness(map);
    }

    @Override
    public List<File> findByBusiness(String businessId) {
        Map map = new HashMap<>();
        map.put("businessId", businessId);
        return somFileMapper.findByBusiness(map);
    }

    /**
     * 根据ID查询附件
     *
     * @param fileId
     * @return
     * @throws Exception
     */
    @Override
    public File queryById(String fileId) throws Exception {
        if (StringUtils.isBlank(fileId)) {
            return null;
        }
        File file = somFileMapper.selectByPrimaryKey(fileId);

        return file;
    }

    /**
     * 根据hbasepath删除附件表记录
     *
     * @param fileHbasePath
     * @return
     */
    @Override
    public int delByFileHbasePath(String fileHbasePath) {
        return somFileMapper.delByFileHbasePath(fileHbasePath);
    }

    /**
     * 插入信息
     *
     * @param file
     * @return
     */
    @Override
    public int insert(File file) {
        return somFileMapper.insert(file);
    }

    /**
     * 上传图片
     *
     * @param request
     * @param fileName
     * @return
     */
    @Override
    public String[] uploadimg(HttpServletRequest request, String fileName) throws Exception {
        String hbasepath = null;
        if (fileName.indexOf(".") > -1) {
            String suffix = fileName.substring(fileName.indexOf("."));
            // java 生成四位随机数
            // int i=(int)(Math.random()*9000)+1000;
            // int i= new java.util.Random().nextInt(9000)+1000;也可以
            hbasepath = DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS_SSS_EN) + "_" + (new Random().nextInt(9000) + 1000) + "_file" + suffix;
            ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
            logger.info("HBase Application Running");
            HbaseUtils userUtils = context.getBean(HbaseUtils.class);
            userUtils.initialize("file");
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[4096];
            int n = 0;
            while (-1 != (n = request.getInputStream().read(buffer))) {
                output.write(buffer, 0, n);
            }
            userUtils.addOrg(hbasepath, output.toByteArray());
            return new String[]{hbasepath, fileName};
        } else {
            return new String[]{"error", fileName};
        }
    }

    /**
     * 根据业务id和类型查询图片路径
     *
     * @param businessId
     * @param type
     * @return
     */
    @Override
    public List<String> findByBusinessId(String businessId, int type) {
        Map map = new HashMap<>();
        map.put("businessId", businessId);
        map.put("type", type);
        return somFileMapper.findByBusinessId(map);
    }
}
